// components/comment-form/comment-form.js
const i18n = require('../../utils/i18n/index');

Component({
  /**
   * Component properties
   */
  properties: {
    placeholder: {
      type: String,
      value: 'Write a comment...'
    },
    buttonText: {
      type: String,
      value: 'Post'
    },
    currentLanguage: {
      type: String,
      value: 'en'
    }
  },

  /**
   * Component initial data
   */
  data: {
    commentText: '',
    t: {} // Translations
  },

  observers: {
    'currentLanguage': function(lang) {
      this.loadLanguage(lang);
    }
  },

  lifetimes: {
    attached: function() {
      this.loadLanguage(this.properties.currentLanguage);
    }
  },

  /**
   * Component methods
   */
  methods: {
    loadLanguage(lang) {
      const translations = i18n.getTranslations(lang);
      this.setData({
        t: translations
      });
    },
    
    onInput(e) {
      this.setData({
        commentText: e.detail.value
      });
    },
    
    onSubmit() {
      const commentText = this.data.commentText.trim();
      if (!commentText) {
        return;
      }
      
      this.triggerEvent('submit', { comment: commentText });
      this.resetFields();
    },
    
    resetFields() {
      this.setData({
        commentText: ''
      });
    }
  }
})